package com.amap.demo.view;

import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.ArcOptions;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.GroundOverlayOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.NavigateArrowOptions;
import com.amap.api.maps.model.PolygonOptions;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.model.TextOptions;
import com.amap.demo.view.util.Constants;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加覆盖物<br />
 * 折线  多边形  圆  图片层  标记  弧形<br />
 * 点击事件
 *
 * @author ys
 */
public class AddOverlayActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, AMap.OnMarkerClickListener, View.OnClickListener, AMap.OnInfoWindowClickListener {

    @Bind(R.id.overlay_mapview)
    MapView mapView;
    @Bind(R.id.overlay_spinner)
    Spinner spinner;
    @Bind(R.id.clear_overlays_btn)
    Button clearOverlaysBtn;

    private AMap aMap;

    private Marker marker1, marker2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_overlay);
        ButterKnife.bind(this);

        mapView.onCreate(savedInstanceState);

        init();
    }

    /**
     * 初始化
     */
    private void init() {
        if (null == aMap) {
            aMap = mapView.getMap();
        }
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.spinner_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);


        //移动地图中心点
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(Constants.SOHO, 16.0f);
        aMap.animateCamera(cameraUpdate);

        //
        aMap.setOnMarkerClickListener(this);
        aMap.setOnInfoWindowClickListener(this);

        setInfoWindowAdapter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                addMarker();
                break;
            case 1:
                addPolyLine();
                break;
            case 2:
                addPolygon();
                break;
            case 3:
                addCircle();
                break;
            case 4:
                addGround();
                break;
            case 5:
                addArc();
                break;
            case 6:
                addText();
                break;
            case 7:
                addNaviArrow();
                break;
            default:
                break;
        }
    }


    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    /**
     * 添加marker
     */
    private void addMarker() {
        aMap.clear();
        //
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.draggable(true);
        markerOptions.title("望京soho");
        markerOptions.visible(true);
        markerOptions.position(Constants.SOHO);
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
        markerOptions.setFlat(true);
        marker1 = aMap.addMarker(markerOptions);

        //
        MarkerOptions markerOptions1 = new MarkerOptions();
        markerOptions.anchor(0.5f, 0.5f);
        ArrayList<BitmapDescriptor> iconList = new ArrayList<>();
        iconList.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        iconList.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
        iconList.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
        iconList.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
        iconList.add(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
        markerOptions1.icons(iconList);
        markerOptions1.period(15);
        markerOptions1.position(Constants.FANGHENG);
        markerOptions1.title("方恒");
        markerOptions1.setInfoWindowOffset(0, -10);//infowindow的偏移量
        marker2 = aMap.addMarker(markerOptions1);
    }


    /**
     * 添加折线
     */
    private void addPolyLine() {
        aMap.clear();
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.add(Constants.QILINSHE).add(Constants.SOHO).add(Constants.LIXING, Constants.YANRAN);
        polylineOptions.color(Color.BLUE);
        polylineOptions.geodesic(true);//大地曲线
        aMap.addPolyline(polylineOptions);

        //
        PolylineOptions polylineOptions1 = new PolylineOptions();
        final List<LatLng> list = new ArrayList<>();
        list.add(Constants.QILINSHE);
        list.add(Constants.BAOLI);
        list.add(Constants.YANRAN);
        Iterable<LatLng> iterable = new Iterable<LatLng>() {
            @Override
            public Iterator<LatLng> iterator() {
                return list.iterator();
            }
        };
        polylineOptions1.color(Color.RED);
        polylineOptions1.width(15);
        polylineOptions1.addAll(iterable);
        polylineOptions1.setDottedLine(true);//画虚线
        aMap.addPolyline(polylineOptions1);

        //
        PolylineOptions polylineOptions2 = new PolylineOptions();
        polylineOptions2.add(Constants.SHOUKAI, Constants.LIXING);
        polylineOptions2.width(30);
        polylineOptions2.setDottedLine(true);
        polylineOptions2.setCustomTexture(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
        aMap.addPolyline(polylineOptions2);
    }

    /**
     * 添加多边形
     */
    private void addPolygon() {
        aMap.clear();
        PolygonOptions polygonOptions = new PolygonOptions();
        polygonOptions.add(Constants.SHOUKAI, Constants.QILINSHE, Constants.YANRAN, Constants.LIXING);
        polygonOptions.fillColor(Color.argb(50, 1, 1, 1));
        polygonOptions.strokeColor(Color.RED).strokeWidth(15);
        aMap.addPolygon(polygonOptions);
    }

    /**
     * 添加圆
     */
    private void addCircle() {
        aMap.clear();

        CircleOptions circleOptions = new CircleOptions();
        circleOptions.center(Constants.SOHO);
        circleOptions.radius(400);
        circleOptions.fillColor(Color.argb(150, 1, 1, 1));
        circleOptions.strokeWidth(20);
        circleOptions.strokeColor(Color.YELLOW);
        aMap.addCircle(circleOptions);
    }

    /**
     * 添加图片层
     */
    private void addGround() {
        aMap.clear();

        GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
        groundOverlayOptions.anchor(0.5f, 0.5f);
        groundOverlayOptions.bearing(30.0f);
        groundOverlayOptions.image(BitmapDescriptorFactory.fromResource(R.mipmap.fdse));
        groundOverlayOptions.position(Constants.QILINSHE, 400, 600);
        groundOverlayOptions.transparency(0.5f);
        groundOverlayOptions.zIndex(10f);
        aMap.addGroundOverlay(groundOverlayOptions);
    }

    /**
     * 添加弧形
     */
    private void addArc() {
        aMap.clear();

        ArcOptions arcOptions = new ArcOptions();
        arcOptions.point(Constants.SHOUKAI, Constants.SOHO, Constants.LIXING);
        arcOptions.strokeWidth(20).strokeColor(Color.BLUE);
        aMap.addArc(arcOptions);
    }

    /**
     * 添加文字覆盖物
     */
    private void addText() {
        aMap.clear();

        TextOptions textOptions = new TextOptions();
        textOptions.text("http://blog.csdn.net/crazy1235");
        textOptions.backgroundColor(Color.WHITE);
        textOptions.fontColor(Color.BLUE);
        textOptions.rotate(30.0f);
        textOptions.fontSize(50);
        textOptions.position(Constants.SOHO);
        aMap.addText(textOptions);
    }

    /**
     * 添加导航箭头
     */
    private void addNaviArrow() {
        aMap.clear();

        NavigateArrowOptions options = new NavigateArrowOptions();
        options.topColor(Color.RED);
        options.width(20.0f);
        options.add(Constants.BAOLI, Constants.SOHO, Constants.QILINSHE);
        aMap.addNavigateArrow(options);
    }


    /**
     *
     */
    private void setInfoWindowAdapter() {
        aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                View infoView = getLayoutInflater().inflate(R.layout.custom_info_window, null);
                render(marker, infoView);
                return infoView;
            }

            @Override
            public View getInfoContents(Marker marker) {
                View infoView = getLayoutInflater().inflate(R.layout.custom_info_window, null);
                render(marker, infoView);
                return infoView;
            }
        });
    }

    /**
     * 设置自定义InfoWindow布局
     *
     * @param marker
     * @param infoView
     */
    private void render(Marker marker, View infoView) {
        ImageView iv = (ImageView) infoView.findViewById(R.id.badge);
        iv.setImageResource(R.drawable.icon_andr);
        TextView tv = (TextView) infoView.findViewById(R.id.title);
        marker.setTitle("http://blog.csdn.net/crazy1235");
        String title = marker.getTitle();
        if (TextUtils.isEmpty(title)) {
            tv.setText("http://blog.csdn.net/crazy1235");
        } else {
            SpannableString ss = new SpannableString(title);
            ss.setSpan(new ForegroundColorSpan(Color.BLUE), 0, ss.length(), 0);
            tv.setText(ss);
        }
    }


    @Override
    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(marker1)) {
            Toast.makeText(AddOverlayActivity.this, marker1.getTitle(), Toast.LENGTH_SHORT).show();
        } else if (marker.equals(marker2)) {
            Toast.makeText(AddOverlayActivity.this, marker2.getTitle(), Toast.LENGTH_SHORT).show();
        }
        return false;
        //返回false的时候会执行默认操作---显示信息窗口
    }

    @OnClick(R.id.clear_overlays_btn)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_overlays_btn:
                aMap.clear();
                break;
        }
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        Toast.makeText(AddOverlayActivity.this, "点击了infoWindow窗口--" + marker.getTitle(), Toast.LENGTH_SHORT).show();
    }
}
